package com.he.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.he.commonutils.R;
import com.he.eduservice.entity.EduTeacher;
import com.he.eduservice.entity.vo.QueryTeacher;
import com.he.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testhejinyu
 * @since 2021-04-12
 */
@Api(description = "讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "获取所有讲师")
    @GetMapping("getAll")
    public R getAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("teachers", list);
    }

    @ApiOperation(value = "根据 id 删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id",value = "讲师id",required = true)
                                  @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/{page}/{limit}")
    public R findPage(
            @ApiParam(name = "page",value = "当前页码",required = true,defaultValue = "1")
            @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页的记录数",required = true)
            @PathVariable("limit") Long limit
    ){
        Page<EduTeacher> teacherPage = new Page<EduTeacher>(page,limit);
        teacherService.page(teacherPage,null);
        List<EduTeacher> list = teacherPage.getRecords();
        Long total = teacherPage.getTotal();
        //也可以按照 Map<String,Object> 来返回
        return R.ok().data("total",total).data("rows",list);
    }

    @ApiOperation(value = "分页查询讲师列表带条件")
    @GetMapping("/getCondition/{page}/{limit}")
    public R findPage(
            @ApiParam(name = "page",value = "当前页码",required = true,defaultValue = "1")
            @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页的记录数",required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(name = "queryTeacher",value = "查询对象",required = false)
            QueryTeacher queryTeacher
    ){
        Page<EduTeacher> teacherPage = new Page<EduTeacher>(page,limit);

        //构建查询条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        //获取 queryTeacher 中的数据
        String name = queryTeacher.getName();
        Integer level = queryTeacher.getLevel();
        String begin = queryTeacher.getBegin();
        String end = queryTeacher.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        teacherService.page(teacherPage,queryWrapper);
        List<EduTeacher> list = teacherPage.getRecords();
        Long total = teacherPage.getTotal();
        //也可以按照 Map<String,Object> 来返回
        return R.ok().data("total",total).data("rows",list);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    public R save(
            @ApiParam(name = "teacher",value = "讲师对象",required = true)
            @RequestBody EduTeacher eduTeacher
                  ){
        teacherService.save(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }
    @ApiOperation(value = "讲师信息修改")
    @PostMapping("/updateTeacher")
    public R getTeacher(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

